package com.shumei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shumei.dao.mapper.ParentCategoryMapper;
import com.shumei.dao.pojo.Eq;
import com.shumei.dao.pojo.ParentCategory;
import com.shumei.service.ParentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ParameterMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ParentCategoryServiceImpl implements ParentCategoryService {
   @Autowired
    ParentCategoryMapper parentCategoryMapper;
    @Override
    public void insertParentCategory(ParentCategory parentCategory) {
        ParentCategory parentCategory1=selectByname(parentCategory.getName());
        if (parentCategory1!=null) {
            System.out.println("父分类名称已存在");
        }
        else {

            int i = parentCategoryMapper.insert(parentCategory);
            if (i > 0) {
                System.out.println("父分类插入成功");

            } else {
                System.out.println("父分类插入失败");

            }
        }
    }

    @Override
    public List<ParentCategory> selectParentCategory() {
        List<ParentCategory> parentCategories=parentCategoryMapper.selectList(null);
        return parentCategories;
    }

    @Override
    public void updateParentCategory(String before,String after) {
        ParentCategory parentCategory1=selectByname(before);

        QueryWrapper<ParentCategory> wrapper=new QueryWrapper<ParentCategory>();
        wrapper.eq("p_id",parentCategory1.getPId());
        ParentCategory parentCategory=new ParentCategory();
        parentCategory.setName(after);
        parentCategory.setUpdateTime(new Date());
        if (parentCategoryMapper.update(parentCategory,wrapper)>0){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }
    }

    @Override
    public void deleteParentCategory(int id) {
        int i = parentCategoryMapper.deleteById(id);
        if (i > 0) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    private   ParentCategory selectByname(String parentname){
        QueryWrapper<ParentCategory> wrapper=new QueryWrapper<ParentCategory>();
        wrapper.eq("name",parentname);
        ParentCategory parentCategory=parentCategoryMapper.selectOne(wrapper);

        return parentCategory;
    }
}
